package com.photoeditor.demo.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.base.utils.DeviceUtils;
import com.common.base.utils.thread.ThreadPool;
import com.common.commercialize.reward.RewardAdManager;
import com.photoeditor.R;
import com.photoeditor.demo.model.preference.SpUtils;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.ui.activity.effect.FilterDetailActivity;
import com.photoeditor.demo.ui.activity.face.FaceLogicActivity;
import com.photoeditor.demo.ui.dialog.IAdVideoDialogListener;
import com.photoeditor.demo.ui.dialog.IDialog;
import com.photoeditor.demo.util.ToastUtil;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

import static com.photoeditor.demo.model.bean.face.BaseFaceResultBean.REPORT_TYPE_FACE_AGING;
import static com.photoeditor.demo.model.preference.SpUtils.ISFROMBUTTON;


/**
 * Author：ZFashion
 * Created Time: 2019.05.30
 */
public class WatchAdvanceObtainResultDialog extends DialogFragment implements View.OnClickListener {

    private static final String EXTRA_KEY_STATISTIC_RETAIN = "extra_key_statistic_retain";
    private static final String KEY_STATISTIC_FUN_PAGE_TYPE = "key_fun_page_type";
    private String mStatisticRetain;
    private int mPageType;
    private static SoftReference<IDialog.IListener> mListener;

    public static WatchAdvanceObtainResultDialog newInstance(String statisticRetain, int pageType, IDialog.IListener listener){
        mListener = new SoftReference<>(listener);
        WatchAdvanceObtainResultDialog dialog = new WatchAdvanceObtainResultDialog();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_KEY_STATISTIC_RETAIN, statisticRetain);
        bundle.putInt(KEY_STATISTIC_FUN_PAGE_TYPE, pageType);
        dialog.setArguments(bundle);return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog == null) {
            return;
        }
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow == null) {
            return;
        }
        WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
        attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(attributes);
        dialogWindow.setBackgroundDrawableResource(R.color.transparent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            dialogWindow.setStatusBarColor(getResources().getColor(R.color.transparent));
        }

        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_watch_advance_obtain_result, container);
        initView(view);
        initArgument();
        return view;
    }

    private void initArgument() {
        if (getArguments() != null){
            mStatisticRetain = getArguments().getString(EXTRA_KEY_STATISTIC_RETAIN);
            mPageType = getArguments().getInt(KEY_STATISTIC_FUN_PAGE_TYPE);
        }
    }

    private void initView(View view) {
        View rlDialogVideo = view.findViewById(R.id.rl_dialog_video);
        View bgDialogVideo = view.findViewById(R.id.img_dialog_video_bg);
        View closeDialogVideo = view.findViewById(R.id.img_dialog_video_close);
        TextView describe = view.findViewById(R.id.tv_dialog_video_describe);
        TextView cancle = view.findViewById(R.id.tv_dialog_video_cancle);
        TextView confirm = view.findViewById(R.id.tv_dialog_video_confirm);

        ViewGroup.LayoutParams rlDialogVidepLp = rlDialogVideo.getLayoutParams();
        rlDialogVidepLp.width = DeviceUtils.dip2px(300);
//        rlDialogVidepLp.height = DeviceUtils.dip2px(318);
        rlDialogVideo.setLayoutParams(rlDialogVidepLp);

//        ViewGroup.LayoutParams bgDialogVideoLp = bgDialogVideo.getLayoutParams();
//        bgDialogVideoLp.height = DeviceUtils.dip2px(174);
//        bgDialogVideo.setLayoutParams(bgDialogVideoLp);

        RelativeLayout.LayoutParams describeLp = (RelativeLayout.LayoutParams) describe.getLayoutParams();
        describeLp.leftMargin = DeviceUtils.dip2px(34);
        describeLp.rightMargin = DeviceUtils.dip2px(34);
        describe.setLayoutParams(describeLp);

        closeDialogVideo.setOnClickListener(this);
        cancle.setOnClickListener(this);
        confirm.setOnClickListener(this);

//        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
//            @Override
//            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_BACK){
//                    dismissAllowingStateLoss();
//                    return true;
//                }
//                return false;
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_dialog_video_close:
                dismissAllowingStateLoss();

                StatisticsUtils.statisticsFaceSecret(StatisticsConstant.STATISTIC_EVENT_FUN_VALUE_RATING,
                        StatisticsConstant.STATISTIC_EVENT_VIP_VALUE_VIDEO_POP,
                        StatisticsConstant.STATISTIC_EVENT_VALUE_QUIT,
                        getStatisticsFunPage());
                if (mListener != null && mListener.get() instanceof IAdVideoDialogListener) {
                    ((IAdVideoDialogListener) mListener.get()).onCloseClick();
                }
                break;
            case R.id.tv_dialog_video_cancle:
                dismissAllowingStateLoss();

                StatisticsUtils.statisticsFaceSecret(StatisticsConstant.STATISTIC_EVENT_FUN_VALUE_RATING,
                        StatisticsConstant.STATISTIC_EVENT_VIP_VALUE_VIDEO_POP,
                        StatisticsConstant.STATISTIC_EVENT_VALUE_QUIT,
                        getStatisticsFunPage());
                if (mListener != null && mListener.get() instanceof IAdVideoDialogListener) {
                    ((IAdVideoDialogListener) mListener.get()).onCancelClick();
                }
                break;
            case R.id.tv_dialog_video_confirm:
                rewardAdVideo();

                StatisticsUtils.statisticsFaceSecret(StatisticsConstant.STATISTIC_EVENT_FUN_VALUE_RATING,
                        StatisticsConstant.STATISTIC_EVENT_VIP_VALUE_VIDEO_POP,
                        StatisticsConstant.STATISTIC_EVENT_FUN_VALUE_CLICK,
                        getStatisticsFunPage());
//                StatisticsUtils.statisticsFaceSecret(StatisticsConstant.STATISTIC_EVENT_KEY_ADVANCE,
//                        StatisticsConstant.STATISTIC_EVENT_KEY_ADVANCE_VALUE_OK,
//                        StatisticsConstant.STATISTIC_EVENT_KEY_ADVANCE_VALUE_CLICK,
//                        getStatisticsFunPage());
                dismissAllowingStateLoss();
                if (mListener != null && mListener.get() instanceof IAdVideoDialogListener) {
                    ((IAdVideoDialogListener) mListener.get()).onSureClick();
                }
                break;
            default:
                break;
        }
    }

    private void rewardAdVideo() {
        Activity activity = getActivity();
        if (activity == null) {
            return;
        }

        RewardAdManager rewardAdManager = null;
        if (activity instanceof EditorImageActivity) {
            rewardAdManager = ((EditorImageActivity) activity).getRewardAdManager();
        } else if (activity instanceof FaceLogicActivity) {
            rewardAdManager = ((FaceLogicActivity) activity).getRewardAdManager();
        }else if (activity instanceof FilterDetailActivity) {
            rewardAdManager = ((FilterDetailActivity) activity).getRewardAdManager();
        }

        if (rewardAdManager == null) {
            ThreadPool.runUITask(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.showToast(R.string.reward_ad_load_failed);
                }
            });
            return;
        }
        SpUtils.obtain().save(ISFROMBUTTON, false);
        rewardAdManager.showRewardAdVideo();
    }

    private String getStatisticsFunPage() {
        String statisticsFunPage = "";
        switch (mPageType){
            case REPORT_TYPE_FACE_AGING:
                statisticsFunPage = StatisticsConstant.STATISTIC_EVENT_FEATURE_VALUE_FEATURE_FACE_AGING;
                break;
        }
        return statisticsFunPage;
    }
}
